using System.Collections.Generic;
using UnityEngine;
using IFramework;
using System;

namespace IFramework
{
    public class UIBase : MonoBehaviour, IUIBase
    {
        public Type ObjType
        {
            get
            {
                return GetType();
            }
        }

        public UIData Data
        {
            get
            {
                return data;
            }
        }

        public object Content
        {
            get
            {
                return this;
            }
        }

        protected UIData data;
        public virtual void OnEnter(params object[] paras)
        {
            SetViewData();
        }
        public virtual void SetViewData()
        {
            data.ReFresh();
        }
        public virtual void OnExit(params object[] paras)
        {
           
        }
        public virtual void OnClean(params object[] paras)
        {
            PoolManager.Instance.Restore((object)this, paras);
            PoolManager.Instance.Clean((object)this, paras);
            helpDic.Clear();
            items.Clear();
            data = null;
        }


        public void OnCreate(params object[] paras)
        {
            items = new Dictionary<Type, UIItem>();
            helpDic = new Dictionary<string, Transform>();
            OnEnter(paras);
        }
        public void OnReuse(params object[] paras)
        {
            OnEnter(paras);
        }
        public void OnRestore(params object[] paras)
        {
            OnExit(paras);
        }


        private Dictionary<Type, UIItem> items;
        public void ShowItem<T>(T Prefab,params object[] paras)where T:UIItem
        {
            if (items.ContainsKey(Prefab.GetType()))
            {
                PoolManager.Instance.GetInitObj(Prefab, this, paras);
            }
            else
            {
                UIItem item = PoolManager.Instance.GetInitObj(Prefab, this, paras);
                item.UIParent = this;
                items.Add(Prefab.GetType(), item);
            }
           
        }
        public void HideItem(Type itemType, params object[] paras)
        {
            PoolManager.Instance.Restore(itemType, items[itemType], this, paras);
        }
        private Dictionary<string, Transform> helpDic;
        public void AddToDic(string name,Transform tran)
        {
            if (helpDic.ContainsKey(name))
            {
                throw new Exception("Change Name");
            }
            helpDic.Add(name, tran);
        }
        public Transform GetCrtlByName(string name)
        {
            if (helpDic.ContainsKey(name))
            {
                return helpDic[name];
            }
            return null;
        }
    }
}